package com.sinoglobal.ningxia.qr;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils.TruncateAt;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.frame.ShareAbstractActivity;
import com.sinoglobal.ningxia.http.ConnectionUtil;
import com.sinoglobal.ningxia.utils.EquipmentUtil;
import com.sinoglobal.ningxia.utils.MContants;
import com.sinoglobal.ningxia.utils.TextUtil;

/**
 * 
 * @author limeishu
 * @createdate 2014-9-4 下午5:00:17
 * @Description:展示扫描网址信息结果界面
 */
public class QrUrlResultActivity extends ShareAbstractActivity implements OnClickListener {
	private TextView refresh;// 刷新
	private TextView copyUrl;// 复制网址
	private TextView openInBrowser;// 在浏览器打开
	private TextView shareCode;// 分享
	private WebView webView;
	private PopupWindow popWindow;
	private View popView;
	private boolean isShare = true;
	private TextView line3;
	private String str_url="";//请求网页地址
	private String titleText="";//页面标题中获取


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_qr_url_result);
		openChenJInShi();//开启沉浸式
		titleView.setText(MContants.EMPTY_STRING);
		// 设置超过10个字，文字省略
		titleView.setSingleLine(true);
		titleView.setMaxEms(10);
		titleView.setEllipsize(TruncateAt.END);
		templateButtonLeft.setOnClickListener(this);
		microButtonRight.setVisibility(View.VISIBLE);
		microButtonRight.setImageResource(R.drawable.qr_icon_more);
		microButtonRight.setPadding(0, 0, 0, 0);
		isShare = getIntent().getBooleanExtra("isShare", true);
		init();
		try {
			WebSettings ws = webView.getSettings();
			ws.setDefaultTextEncodingName("utf-8");

			ws.setBlockNetworkImage(false);
			ws.setBlockNetworkLoads(false);
			ws.setPluginState(PluginState.ON);
			ws.setJavaScriptEnabled(true);// 设置支持javascript

			webView.requestFocus();// 获取触摸焦点
			webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);// 取消滚动条
			webView.getSettings().setBuiltInZoomControls(true); // 构建缩放控制
			webView.getSettings().setSupportZoom(true); // 设置支持缩放
			webView.loadUrl(str_url);
			webView.setDownloadListener(new DownloadListener() {

				@Override
				public void onDownloadStart(String url, String arg1, String arg2, String arg3, long arg4) {
					Uri uri = Uri.parse(url);
					Intent intent = new Intent(Intent.ACTION_VIEW, uri);
					intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
					startActivity(intent);
					finish();
				}
			});
			webView.setWebViewClient(new WebViewClient() {

				@Override
				public boolean shouldOverrideUrlLoading(WebView view, String url) {
					try {
						if (url.indexOf("tel:") == 0) {

							Intent call_intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
							call_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							if (call_intent != null) {
								startActivity(call_intent);
							}

						} else if (url.contains("@")) {
							Intent intent = new Intent();
							intent.setAction(Intent.ACTION_SENDTO);
							intent.setData(Uri.parse(url));
							startActivity(intent);
						} else {
							str_url=url;
							view.loadUrl(url);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					return true;
				}
			});
			webView.setWebChromeClient(new WebChromeClient() {
				@Override
				public void onReceivedTitle(WebView view, String title) {
					super.onReceivedTitle(view, title);
					titleView.setText(title);
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressLint("InlinedApi")
	private void init() {
		str_url=getIntent().getStringExtra("captureResult");
		titleText=getIntent().getStringExtra("intro");
		showShortToastMessage(getString(R.string.qr_url_loading));
		webView = (WebView) findViewById(R.id.qr_result_web);
		CookieSyncManager.createInstance(QrUrlResultActivity.this);
		CookieSyncManager.getInstance().startSync();
		CookieManager.getInstance().removeSessionCookie();
		webView.clearCache(true);
		webView.clearHistory();
		popView = LayoutInflater.from(this).inflate(R.layout.qr_choose_dialog, null);
		refresh = (TextView) popView.findViewById(R.id.refresh);
		copyUrl = (TextView) popView.findViewById(R.id.copy_url);
		openInBrowser = (TextView) popView.findViewById(R.id.open_browser);
		shareCode = (TextView) popView.findViewById(R.id.share_url);
		line3 = (TextView) popView.findViewById(R.id.line3);
		if (isShare) {
			shareCode.setVisibility(View.VISIBLE);
			line3.setVisibility(View.VISIBLE);
		} else {
			shareCode.setVisibility(View.GONE);
			line3.setVisibility(View.GONE);
		}
		popWindow = new PopupWindow(popView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(getResources().getColor(R.color.transparent));
		// 需要设置一下此参数，点击外边可消失
		popWindow.setBackgroundDrawable(dw);
		// 设置点击窗口外边窗口消失
		popWindow.setOutsideTouchable(true);
		// 设置此参数获得焦点，否则无法点击
		popWindow.setFocusable(true);

		microButtonRight.setOnClickListener(this);
		refresh.setOnClickListener(this);
		copyUrl.setOnClickListener(this);
		openInBrowser.setOnClickListener(this);
		shareCode.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_but_left:
			if (webView.canGoBack()) {
				webView.goBack();
			} else {
				finish();
			}
			break;
		case R.id.mico_but_right:
			popWindow.showAsDropDown(microButtonRight, 0, EquipmentUtil.px2dip(QrUrlResultActivity.this, 23));
			break;
		case R.id.refresh:// 点击刷新
			webView.loadUrl(str_url);
			popWindow.dismiss();
			break;
		case R.id.copy_url:// 点击复制网址
			try {
				TextUtil.copy(str_url, getApplicationContext());
				showShortToastMessage(getString(R.string.qr_url_copy_success));
				popWindow.dismiss();
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case R.id.open_browser:// 在浏览器中打开
			Uri uri = Uri.parse(str_url);
			Intent intent = new Intent(Intent.ACTION_VIEW, uri);//
			startActivity(intent);
			popWindow.dismiss();
			break;
		case R.id.share_url:
			// 分享
//			showPopwindow(true, titleText!=null?titleText:""+str_url + getString(R.string.share_postfix)
//					+ ConnectionUtil.MicDoorAdress, null, ConnectionUtil.MicDoorAdress);
			showPopwindow(true, titleText!=null?titleText:""+str_url + ConnectionUtil.MicDoorAdress, null, str_url);
			popWindow.dismiss();
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (webView.canGoBack()) {
				webView.goBack();
			} else {
				finish();
			}
		}
		return false;
	}

}
